from rest_framework import serializers


from MirrorCore.models.Content import Essay
from MirrorCore.serializers.BadgeSeriralizer import EssayBadgeSerializer
from MirrorCore.serializers.CollectionSerializer import CollectionSerializer
from MirrorCore.serializers.utils import TimestampField




class EssaySerializer(serializers.ModelSerializer):
    collection = CollectionSerializer(read_only=True)
    # collection对应id，用于更新
    collection_id = serializers.IntegerField(write_only=True,allow_null=True,required=False)

    badges = EssayBadgeSerializer(many=True,read_only=True)
    create_time = TimestampField(required=False)
    update_time = TimestampField(required=False,read_only=True)
    author_nickname = serializers.CharField(source='author.nickname', read_only=True)
    author_photo = serializers.CharField(source='author.photo', read_only=True)

    class Meta:
        model = Essay
        fields = ['id','title','content','collection','collection_id','badges','author','author_nickname','author_photo', 'create_time','update_time','is_active']

    def create(self, validated_data):
        print(validated_data)
        return super().create(validated_data)
